/**
 * 
 */
package command.code;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

import quantum.error.correcting.code.finalone.Code;
import quantum.error.correcting.code.finalone.CodeComparator;
import quantum.error.correcting.code.finalone.CodeLenComparator;
import quantum.error.correcting.code.finalone.CodeUtil;

/**
 * @author sukani
 * 
 */
public class MainCommander {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			new MainCommander().run();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void run() throws IOException {
		String line = "";
		int len, m_num, ab_num;
		Code M[], X[], Z[], N[];
		System.out.println("Pls input the length of M:");
		BufferedReader bufReader = new BufferedReader(new InputStreamReader(
				System.in));
		line = bufReader.readLine();
		len = Integer.parseInt(line);
		System.out.println("Pls input the number of M:");
		bufReader = new BufferedReader(new InputStreamReader(System.in));
		line = bufReader.readLine();
		m_num = Integer.parseInt(line);
		M = new Code[m_num];
		System.out.println("Pls input M(1.." + (m_num) + "):");
		for (int i = 0; i < m_num; i++) {
			System.out.print("M" + (i + 1) + "=");
			bufReader = new BufferedReader(new InputStreamReader(System.in));
			line = bufReader.readLine();
			M[i] = new Code(line);
			M[i].setCodeName("M" + (i + 1));
		}
		System.out.println("Pls input the number of X or Z:");
		bufReader = new BufferedReader(new InputStreamReader(System.in));
		line = bufReader.readLine();
		ab_num = Integer.parseInt(line);

		X = new Code[ab_num];
		Z = new Code[ab_num];
		System.out.println("Pls input X(1.." + (ab_num) + "):");

		for (int i = 0; i < ab_num; i++) {
			System.out.print("X" + (i + 1) + "=");
			bufReader = new BufferedReader(new InputStreamReader(System.in));
			line = bufReader.readLine();
			X[i] = new Code(line);
			X[i].setCodeName("X" + (i + 1));
		}
		System.out.println("Pls input Z(1.." + (ab_num) + "):");
		for (int i = 0; i < ab_num; i++) {
			System.out.print("Z" + (i + 1) + "=");
			bufReader = new BufferedReader(new InputStreamReader(System.in));
			line = bufReader.readLine();
			Z[i] = new Code(line);
			Z[i].setCodeName("Z" + (i + 1));
		}

		Code unionM[] = CodeUtil.getUnionAll(M, len / 2);

		int cou = 0;
		N = new Code[ab_num * 2 + m_num];
		for (int i = 0; i < m_num; i++) {
			N[cou++] = M[i];
		}
		for (int i = 0; i < ab_num; i++) {
			N[cou++] = X[i];
			N[cou++] = Z[i];
		}

		Code full[] = CodeUtil.getUnionAll(N, len / 2);

		StringBuilder firstResult = new StringBuilder();
		StringBuilder secondResult = new StringBuilder();
		Arrays.sort(full, new CodeComparator());
		for (int i = 0; i < full.length; i++) {
			int l = CodeUtil.getCodeLen(full[i]);
			firstResult.append("[" + full[i].getCodeName() + "]:\t" + l+"\n");
		}

		Arrays.sort(full, new CodeLenComparator());
		for (int i = 0; i < full.length; i++) {
			// int l=CodeUtil.getCodeLen(full[i]);
			secondResult.append(full[i].getCodeLen() + ":\t" + "["
					+ full[i].getCodeName() + "]"+"\n");
		}
		/* Check */
		for (int i = 0; i < m_num; i++) {
			if (M[i].getLen() != len) {
				System.out.println("INPUT ERROR!!!---------M LEN");
				return;
			}
		}
		for (int i = 0; i < ab_num; i++) {
			if (X[i].getLen() != len) {
				System.out.println("INPUT ERROR!!!---------X LEN");
				return;
			}
			if (Z[i].getLen() != len) {
				System.out.println("INPUT ERROR!!!---------Z LEN");
				return;
			}
		}
		/*---*/
		File first = new File("first.txt");
		File second = new File("second.txt");
		FileWriter filewriter = new FileWriter(first, false);
		filewriter.write(firstResult.toString());
		filewriter.close();
		filewriter = new FileWriter(second, false);
		filewriter.write(secondResult.toString());
		filewriter.close();
		System.out.println("Result is saved at: " + first.getAbsolutePath()
				+ " and " + second.getAbsolutePath() + ".");

	}

}
